package com.cargohu.aishouyu.app;

import com.cargohu.aishouyu.common.CommonConstants;
import com.cargohu.aishouyu.framework.page.Paging;
import com.cargohu.aishouyu.framework.response.ApiResult;
import com.cargohu.aishouyu.member.dto.*;
import com.cargohu.aishouyu.member.req.ChatHistoryRequest;
import com.cargohu.aishouyu.member.resp.ChatHistoryResponse;
import com.cargohu.aishouyu.member.resp.ChatPhraseLabelDetailVo;
import com.cargohu.aishouyu.member.resp.MemberChatPhraseVo;
import com.cargohu.aishouyu.member.resp.MemberDetailVo;
import com.cargohu.aishouyu.member.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * APP会员接口
 * </p>
 *
 * @author SuperWein
 * @since 2023/12/2 17:06
 */
@Slf4j
@RestController
@RequestMapping(CommonConstants.API_APP_PATH+"/member")
@Tag(name = "APP会员接口(移动端)")
public class AppMemberController {

    @Autowired
    private MemberSignService memberSignService;
    @Autowired
    private ChatPhraseService chatPhraseService;
    @Autowired
    private ChatPhraseLabelService chatPhraseLabelService;
    @Autowired
    private ChatHistoryService chatHistoryService;
    @Autowired
    private MemberService memberService;

    /**
     * 添加会员表
     *
     * @param memberAddDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @Operation(summary = "添加会员表")
    public ApiResult addMember(@Valid @RequestBody MemberAddDTO memberAddDTO) throws Exception {
        boolean flag = memberService.addMember(memberAddDTO);
        return ApiResult.result(flag);
    }

    /**
     * 修改会员表
     *
     * @param memberUpdateDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    @Operation(summary = "修改会员表")
    public ApiResult updateMember(@Valid @RequestBody MemberUpdateDTO memberUpdateDTO) throws Exception {
        boolean flag = memberService.updateMember(memberUpdateDTO);
        return ApiResult.result(flag);
    }

    /**
     * 获取会员表详情
     *
     * @param memberId
     * @return
     * @throws Exception
     */
    @PostMapping("/get/{memberId}")
    @Operation(summary = "会员表详情")
    public ApiResult<MemberDetailVo> getMember(@PathVariable Long memberId) throws Exception {
        MemberDetailVo memberDetailVo = memberService.getMemberById(memberId);
        return ApiResult.success(memberDetailVo);
    }

    /**
     * 用户签到
     *
     * @param memberId 会员ID
     * @return ApiResult
     */
    @PostMapping("/sign/{memberId}")
    @Operation(summary = "用户签到")
    public ApiResult memberSign(@PathVariable Long memberId) throws Exception {
        boolean flag = memberSignService.addMemberSign(new MemberSignAddDTO().setMemberId(memberId));
        return ApiResult.result(flag);
    }

    /**
     * 用户本周签到记录
     *
     * @param memberId
     * @return
     * @throws Exception
     */
    @PostMapping("/week-sign-history/{memberId}")
    @Operation(summary = "用户本周签到记录")
    public ApiResult<List<Integer>> getMemberWeekSignHistory(@PathVariable Long memberId) throws Exception {
        List<Integer> memberWeekSignList = memberSignService.getMemberWeekSignHistory(memberId);
        return ApiResult.success(memberWeekSignList);
    }

    /**
     * 聊天常用语标签
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/phrase-label")
    @Operation(summary = "聊天常用语标签")
    public ApiResult<ChatPhraseLabelDetailVo> getChatPhraseLabel() throws Exception {
        List<ChatPhraseLabelDetailVo> chatPhraseLabelList = chatPhraseLabelService.getAllChatPhraseLabelList();
        return ApiResult.success(chatPhraseLabelList);
    }

    /**
     * 用户聊天常用语
     *
     * @param memberId
     * @return
     * @throws Exception
     */
    @PostMapping("/phrase/{memberId}")
    @Operation(summary = "用户聊天常用语")
    public ApiResult<List<MemberChatPhraseVo>> getMemberChatPhrase(@PathVariable Long memberId) throws Exception {
        List<MemberChatPhraseVo> chatPhraseDetailVoList = chatPhraseService.getChatPhraseByMemberId(memberId);
        return ApiResult.success(chatPhraseDetailVoList);
    }

    /**
     * 新增聊天常用语
     *
     * @param chatPhraseAddDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/phrase/add")
    @Operation(summary = "新增聊天常用语")
    public ApiResult addChatPhrase(@Valid @RequestBody ChatPhraseAddDTO chatPhraseAddDTO) throws Exception {
        boolean flag = chatPhraseService.addChatPhrase(chatPhraseAddDTO);
        return ApiResult.result(flag);
    }

    /**
     * 修改聊天常用语
     *
     * @param chatPhraseUpdateDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/phrase/update")
    @Operation(summary = "修改聊天常用语")
    public ApiResult updateChatPhrase(@Valid @RequestBody ChatPhraseUpdateDTO chatPhraseUpdateDTO) throws Exception {
        boolean flag = chatPhraseService.updateChatPhrase(chatPhraseUpdateDTO);
        return ApiResult.result(flag);
    }

    /**
     * 删除聊天常用语
     *
     * @param id 常用语ID
     * @return
     * @throws Exception
     */
    @PostMapping("/phrase/delete/{id}")
    @Operation(summary = "删除聊天常用语")
    public ApiResult deleteChatPhrase(@PathVariable Long id) throws Exception {
        boolean flag = chatPhraseService.deleteChatPhrase(id);
        return ApiResult.result(flag);
    }

    /**
     * 添加聊天记录
     *
     * @param chatHistoryAddDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/chat-history/add")
    @Operation(summary = "添加聊天记录")
    public ApiResult addChatHistory(@Valid @RequestBody ChatHistoryAddDTO chatHistoryAddDTO) throws Exception {
        boolean flag = chatHistoryService.addChatHistory(chatHistoryAddDTO);
        return ApiResult.result(flag);
    }

    /**
     * 删除聊天记录
     *
     * @param id
     * @return
     * @throws Exception
     */
    @PostMapping("/chat-history/delete/{id}")
    @Operation(summary = "删除聊天记录")
    public ApiResult deleteChatHistory(@PathVariable Long id) throws Exception {
        boolean flag = chatHistoryService.deleteChatHistory(id);
        return ApiResult.result(flag);
    }

    /**
     * 分页查询聊天记录
     *
     * @param chatHistoryRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/chat-history/page")
    @Operation(summary = "分页查询聊天记录")
    public ApiResult<ChatHistoryResponse> getChatHistoryList(@Valid @RequestBody ChatHistoryRequest chatHistoryRequest) throws Exception {
        if (chatHistoryRequest.getMemberId() == null) {
            return ApiResult.fail("会员ID不能为空");
        }
        Paging<ChatHistoryResponse> paging = chatHistoryService.getChatHistoryList(chatHistoryRequest);
        return ApiResult.success(paging);
    }

}
